//
//  MineViewController.m
//  Shine
//
//  Created by 王斌 on 16/3/9.
//  Copyright © 2016年 王斌. All rights reserved.
//

#import "MineViewController.h"
#import "MineLeftHeadCVCell.h"
#import "MineMidHeadCVCell.h"
#import "MineRightHeadCVCell.h"
#import "MineNoSetTVCell.h"
#import "MineSetTVCell.h"
#import "MineWeightDatePickerView.h"
#import "YJW_DatePickerView.h"
#import "MineTargetWeightTVCell.h"
#import "MineTargetWeightView.h"
#import "MineBMIView.h"
//折线图
#import "PNChart.h"

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate, UITextFieldDelegate>
@property(nonatomic, strong)UITableView *tableViewMine;
@property(nonatomic, strong)UICollectionView *collectionViewHead;
@property(nonatomic, strong)UIView *viewHead;
@property(nonatomic, strong)UIPageControl *pageControlHead;
@property(nonatomic, assign)BOOL isSelected;
@property(nonatomic, strong)MineWeightDatePickerView *viewSet;
@property(nonatomic, strong)YJW_DatePickerView *datePickerView;
@property(nonatomic, strong)MineTargetWeightView *targetView;
@property(nonatomic, strong)CoreDataManager *coreDataManager;
@property(nonatomic, strong)NSArray *coreArr;
@property(nonatomic, assign)NSInteger count;
@property(nonatomic, strong)MineBMIView *BMIView;
@property(nonatomic, strong)PNLineChart * lineChart;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我";
    
    // 创建tableView
    [self createTableViewMine];
    
    // 创建tableView头视图
    [self createTableViewHead];
    
    // 在self.view上添加个手势
    UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
    [self.view addGestureRecognizer:tapView];
    
    // 初始化coreData
    self.coreDataManager = [CoreDataManager shareInstance];
    
    // 获取步数
    [self createStepNumber];
    
    // 如果程序设置完体重的button再运行程序_isSelected = YES, 也就是创建第二个cell
    NSString *str = [self sandBoxWeight];
    if (str == nil) {
        _isSelected = NO;
    } else {
        _isSelected = YES;
    }
}

#pragma mark 获取步数
- (void)createStepNumber{
    self.coreArr = [self.coreDataManager selectSevenDay];
    self.count = 0;
    for (HealthKit *temp in self.coreArr) {
        self.count += [temp.count integerValue];
    }
}

# pragma mark 在self.view上添加个手势
- (void)tapView:(UITapGestureRecognizer *)tap{
    [self.viewSet removeFromSuperview];
    UIVisualEffectView *effect = (UIVisualEffectView *)[self.view viewWithTag:1001];
    [effect removeFromSuperview];
    [self.targetView removeFromSuperview];
    [self.BMIView removeFromSuperview];
}

# pragma mark 创建头视图(UICollectionView)
- (void)createTableViewHead{
    
    self.viewHead = [[UIView alloc] init];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(WIDTH_SCREEN, 200);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumLineSpacing = 0;
    
    self.collectionViewHead = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, 200) collectionViewLayout:flow];
    self.collectionViewHead.delegate = self;
    self.collectionViewHead.dataSource = self;
    self.collectionViewHead.contentOffset = CGPointMake(WIDTH_SCREEN, 0);
    self.collectionViewHead.pagingEnabled = YES;
    self.collectionViewHead.bounces = NO;
    self.collectionViewHead.showsHorizontalScrollIndicator = NO;
    [self.collectionViewHead registerClass:[MineLeftHeadCVCell class] forCellWithReuseIdentifier:@"left"];
    [self.collectionViewHead registerClass:[MineMidHeadCVCell class] forCellWithReuseIdentifier:@"mid"];
    [self.collectionViewHead registerClass:[MineRightHeadCVCell class] forCellWithReuseIdentifier:@"right"];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self.collectionViewHead scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    [self.viewHead addSubview:self.collectionViewHead];
    self.tableViewMine.tableHeaderView = self.viewHead;
    self.pageControlHead = [[UIPageControl alloc] init];
    self.pageControlHead.numberOfPages = 3;
    self.pageControlHead.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControlHead.currentPageIndicatorTintColor = COLORYELLO;
    [self.viewHead addSubview:self.pageControlHead];
    
    [self.pageControlHead addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    
    [self.viewHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-170);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(WIDTH_SCREEN);
        make.height.mas_equalTo(200);
    }];
    
    [self.collectionViewHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.viewHead);
    }];
    
    [self.pageControlHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(180);
        make.centerX.mas_equalTo(self.view.center.x).with.offset(0);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(20);
    }];
}

# pragma mark collectionViewHead滑动结束page跟着变
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.pageControlHead.currentPage = self.collectionViewHead.contentOffset.x/WIDTH_SCREEN;
}

# pragma mark pageControlHead的方法, 当page改变collectionViewHead的偏移量也变
- (void)pageChange:(UIPageControl *)page{
    self.collectionViewHead.contentOffset = CGPointMake(page.currentPage*WIDTH_SCREEN, 0);
}

# pragma mark 创建TableView
- (void)createTableViewMine{
    self.tableViewMine = [[UITableView alloc] init];
    [self.view addSubview:self.tableViewMine];
    [self.tableViewMine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(WIDTH_SCREEN);
        make.height.mas_equalTo(HEIGHT_SCREEN);
    }];
    self.tableViewMine.backgroundColor = COLORBLACK;
    self.tableViewMine.delegate = self;
    self.tableViewMine.dataSource = self;
    self.tableViewMine.contentInset = UIEdgeInsetsMake(200, 0, 120, 0);
    self.tableViewMine.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableViewMine.showsVerticalScrollIndicator = NO;
    
    [self.tableViewMine registerClass:[MineNoSetTVCell class] forCellReuseIdentifier:@"reuse"];
    
    [self.tableViewMine registerClass:[MineSetTVCell class] forCellReuseIdentifier:@"set"];
    
    [self.tableViewMine registerClass:[MineTargetWeightTVCell class] forCellReuseIdentifier:@"target"];
}

# pragma mark collectionView  item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}

# pragma mark collectionView赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == 0) {
        MineLeftHeadCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"left" forIndexPath:indexPath];
        if (self.count / 38.0 <= 904) {
            cell.titleLabel.text = @"加油阿 ! 徐徐图之, 乃真达标也!";
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.objectivesLabel.text = @"基础目标:904大卡";
            cell.objectivesLabel.textColor = [UIColor lightGrayColor];
            cell.progressView.progress = self.count / 38.0 / 904;
        } else if(self.count / 38.0 > 904 && self.count / 38.0 <= 2034){
            cell.titleLabel.text = @"加油阿 ! 准达标, 出关在望~";
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.objectivesLabel.text = @"基础目标:2034大卡";
            cell.objectivesLabel.textColor = [UIColor lightGrayColor];
            cell.progressView.progress = self.count / 38.0 / 2034;
        } else if (self.count / 38.0 > 2034 && self.count / 38.0 <= 4069){
            cell.titleLabel.text = @"没谁了袄 ! 继续努力 ! ";
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.objectivesLabel.text = @"基础目标:4069大卡";
            cell.objectivesLabel.textColor = [UIColor lightGrayColor];
            cell.progressView.progress = self.count / 38.0 / 4069;
        } else if (self.count / 38.0 > 4069 && self.count / 38.0 <= 8139){
            cell.titleLabel.text = @"莫非您就是传说中的超人嘛~";
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.objectivesLabel.text = @"基础目标:8139大卡";
            cell.objectivesLabel.textColor = [UIColor lightGrayColor];
            cell.progressView.progress = self.count / 38.0 / 8139;
        } else {
            cell.titleLabel.text = @"加油火星人~";
            cell.titleLabel.textColor = [UIColor lightGrayColor];
            cell.objectivesLabel.text = @"运动永无止境";
            cell.objectivesLabel.textColor = [UIColor lightGrayColor];
            cell.progressView.progress = self.count / 38.0 / 1000000;
        }
        cell.kcalNumberLabel.text = [NSString stringWithFormat:@"%.f", self.count / 38.0];
        cell.kcalNumberLabel.textColor = COLORYELLO;
        cell.kcalLabel.text = @"大卡--本周";
        cell.kcalLabel.textColor = [UIColor whiteColor];
        cell.progressView.progressTintColor = COLORYELLO;
        cell.progressView.trackTintColor = [UIColor lightGrayColor];
        cell.progressView.progressViewStyle = UIProgressViewStyleBar;
        cell.contentView.backgroundColor = COLORBLACK;
        return cell;
    } else if(indexPath.item == 1){
        MineMidHeadCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mid" forIndexPath:indexPath];
        cell.leftTopLabel.text = @"最近您击败了";
        cell.leftTopLabel.textColor = [UIColor lightGrayColor];
        cell.leftMidLabel.text = [NSString stringWithFormat:@"%d%d%%", arc4random() % (9-5+1)+5, arc4random() % 10];
        cell.leftMidLabel.textColor = COLORYELLO;
        cell.leftMidLabel.font = [UIFont systemFontOfSize:40];
        cell.leftBottomLabel.text = @"的全国用户";
        cell.leftBottomLabel.textColor = [UIColor lightGrayColor];
        cell.rightTopLabel.text = @"您的活力指数";
        cell.rightTopLabel.textColor = [UIColor lightGrayColor];
        cell.rightMidLabel.text = [NSString stringWithFormat:@"2%d岁", arc4random() % 10];
        cell.rightMidLabel.font = [UIFont systemFontOfSize:40];
        cell.rightMidLabel.textColor = COLORYELLO;
        cell.rightBottomLabel.text = @"燃烧吧查克拉";
        cell.rightBottomLabel.textColor = [UIColor lightGrayColor];
        cell.midLineView.backgroundColor = [UIColor blackColor];
        cell.contentView.backgroundColor = COLORBLACK;
        return cell;
    } else {
        MineRightHeadCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"right" forIndexPath:indexPath];
        cell.totalLabel.text = @"生涯合计";
        cell.totalLabel.textColor = [UIColor lightGrayColor];
        cell.stepNumberLabel.text = [NSString stringWithFormat:@"%ld", self.count];
        cell.stepNumberLabel.font = [UIFont systemFontOfSize:40];
        cell.stepNumberLabel.textColor = COLORYELLO;
        cell.stepLabel.text = @"步";
        cell.stepLabel.textColor = [UIColor lightGrayColor];
        cell.kilocalorieLabel.text = @"大卡";
        cell.kilocalorieLabel.textColor = [UIColor lightGrayColor];
        // 大卡赋值
        cell.kilocalorieNumberLabel.text = [NSString stringWithFormat:@"%.f", self.count / 38.0];
        cell.kilocalorieNumberLabel.font = [UIFont systemFontOfSize:30];
        cell.kilocalorieNumberLabel.textColor = COLORYELLO;
        cell.kilometreLabel.text = @"公里";
        cell.kilometreLabel.textColor = [UIColor lightGrayColor];
        // 公里
        cell.kilometreNumberLabel.text = [NSString stringWithFormat:@"%ld", self.count / 1464];
        cell.kilometreNumberLabel.textColor = COLORYELLO;
        cell.kilometreNumberLabel.font = [UIFont systemFontOfSize:30];
        cell.hourLabel.text = @"小时";
        cell.hourLabel.textColor = [UIColor lightGrayColor];
        // 时间
        NSInteger h = self.count / 104 / 60;
        cell.hourNumberLabel.text = [NSString stringWithFormat:@"%ld", h];
        cell.hourNumberLabel.textColor = COLORYELLO;
        cell.hourNumberLabel.font = [UIFont systemFontOfSize:30];
        cell.contentView.backgroundColor = COLORBLACK;
        return cell;
    }
}

# pragma mark tableView行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isSelected) {
        if (indexPath.row == 0) {
            return 175;
        } else {
            return 210;
        }
    } else {
        return HEIGHT_SCREEN-200-44-64;
    }
}

# pragma mark tableView行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_isSelected) {
        return 2;
    } else {
        return 1;
    }
}

# pragma mark tableView赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!_isSelected) {
        MineNoSetTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
            cell.noWeightLabel.text = @"您还没有记录体重";
            cell.noWeightLabel.textColor = [UIColor lightGrayColor];
            cell.personImageView.image = [UIImage imageNamed:@"weight_empty_pic.png"];
            [cell.buttonSetWeight setImage:[UIImage imageNamed:@"weight_input_icon_grey@2x.png"] forState:UIControlStateNormal];
            [cell.buttonSetWeight addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            cell.imageViewLine.image = [UIImage imageNamed:@"weight_descriptionline@3x.png"];
            cell.contentView.backgroundColor = COLORBLACK;
            cell.tag = 1000;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        if (indexPath.row == 0) {
            MineSetTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"set" forIndexPath:indexPath];
            cell.nowWeightLabel.text = @"当前体重";
            cell.nowWeightLabel.textColor = [UIColor lightGrayColor];
            cell.nowWeightNumberLabel.tag = 4000;
#warning 在这调用sandBox的方法给label赋值
            cell.nowWeightNumberLabel.text = [self sandBoxWeight];
            CGFloat weightSet = [[self sandBoxWeight] floatValue];
            cell.nowWeightNumberLabel.textColor = COLORYELLO;
            cell.nowWeightNumberLabel.font = [UIFont systemFontOfSize:40];
#warning 给nowWeightNumberLabel添加双击手势
            cell.nowWeightNumberLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAction:)];
            // 把手势添加到视图上去
            [cell.nowWeightNumberLabel addGestureRecognizer:tap];
            // 触发方法需要点击几次,默认是1
            tap.numberOfTapsRequired = 2;
            [cell.buttonSetWeight setImage:[UIImage imageNamed:@"weight_input_icon_grey@2x.png"] forState:UIControlStateNormal];
            [cell.buttonSetWeight addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            cell.hiddenLabel.tag = 2000;
            cell.hiddenLabel.textColor = [UIColor whiteColor];
            cell.hiddenLabel.textAlignment = YES;
            [cell.buttonWeightChange setTitle:@"无变化" forState:UIControlStateNormal];
            [cell.buttonWeightChange setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [cell.buttonWeightChange addTarget:self action:@selector(clickLookChange:) forControlEvents:UIControlEventTouchUpInside];
            cell.buttonWeightChange.tag = 1500;
            cell.buttonWeightChange.backgroundColor = [UIColor colorWithRed:49/256.0 green:141/256.0 blue:63/256.0 alpha:1];
            cell.buttonWeightChange.layer.cornerRadius = 3;
            cell.buttonWeightChange.layer.masksToBounds = YES;
            cell.buttonWeightChange.showsTouchWhenHighlighted=YES;
             CGFloat heightSet = [[self  sandBoxHeight] floatValue];
            [cell.bmiButton setTitle:[NSString stringWithFormat:@"BMI %.1f", weightSet /((heightSet/100)*(heightSet/100))] forState:UIControlStateNormal];
            [cell.bmiButton addTarget:self action:@selector(bmiAction:) forControlEvents:UIControlEventTouchUpInside];
            cell.contentView.backgroundColor = COLORBLACK;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        } else {
            MineTargetWeightTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"target" forIndexPath:indexPath];
            cell.targetButton.tag = 6001;
            [cell.targetButton setTitle:@"设置目标体重" forState:UIControlStateNormal];
            [cell.targetButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [cell.targetButton addTarget:self action:@selector(targetAction:) forControlEvents:UIControlEventTouchUpInside];
            [cell.imageButton setImage:[UIImage imageNamed:@"weight_setgoal_icon@2x.png"] forState:UIControlStateNormal];
            [cell.imageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [cell.imageButton addTarget:self action:@selector(targetAction:) forControlEvents:UIControlEventTouchUpInside];
            
            NSString *targetStr = [self sandBoxTargetWeight];
            NSInteger targetInt = [targetStr integerValue];
            cell.targetWeightY = targetInt;
            NSLog(@"---------%ld", cell.targetWeightY);
            
            NSString *nowStr = [self sandBoxWeight];
            NSInteger nowInt = [nowStr integerValue];
            cell.nowWeightY = nowInt;
            NSLog(@"+++++++%ld", cell.nowWeightY);
            
            NSString *timeStr = [self sandBoxTargetTime];
            NSInteger timeInt = [timeStr integerValue];
            cell.timeInt = timeInt;
            NSLog(@"!!!!!!!!!%ld", cell.timeInt);
            
            cell.contentView.backgroundColor = COLORBLACK;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
}

#pragma mark BMIbutton点击方法
- (void)bmiAction:(UIButton *)button{
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = [UIScreen mainScreen].bounds;
    effectView.tag = 1001;
    effectView.alpha = 0.8;
    [self.view addSubview:effectView];
    __weak typeof (self.view)weakView = self.view;
    self.BMIView = [[MineBMIView alloc] init];
    self.BMIView.layer.cornerRadius = 10;
    self.BMIView.layer.borderWidth = 2;
    self.BMIView.layer.masksToBounds = YES;
    self.BMIView.heightNumberTextField.tag = 3000;
    self.BMIView.heightNumberTextField.delegate = self;
    self.BMIView.heightNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.BMIView.xButton addTarget:self action:@selector(xAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.BMIView.saveButton addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.BMIView];
    [self.BMIView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakView.mas_centerY).with.offset(-50);
        make.centerX.mas_equalTo(weakView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(250, 160));
    }];
}

#pragma mark 设置目标体重的button点击方法
- (void)targetAction:(UIButton *)button{
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = [UIScreen mainScreen].bounds;
    effectView.tag = 1001;
    effectView.alpha = 0.8;
    [self.view addSubview:effectView];
    __weak typeof (self.view)weakView = self.view;
    self.targetView = [[MineTargetWeightView alloc] init];
    self.targetView.layer.cornerRadius = 10;
    self.targetView.layer.borderWidth = 2;
    self.targetView.layer.masksToBounds = YES;
    self.targetView.textFieldNumber.delegate = self;
    self.targetView.textFieldNumber.keyboardType = UIKeyboardTypeNumberPad;
    self.targetView.textFieldNumber.tag = 8000;
    self.targetView.timeNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.targetView.timeNumberTextField.tag = 8001;
    [self.targetView.xButton addTarget:self action:@selector(xAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.targetView.saveButton addTarget:self action:@selector(saveLineAction:) forControlEvents:UIControlEventTouchUpInside];
#warning 调用[self sandBoxTargetWeight]方法赋值
    NSString *targetWeightStr = [self sandBoxTargetWeight];
    NSString *targetTimeStr = [self sandBoxTargetTime];
    NSString *nowWeightStr = [self sandBoxWeight];
    CGFloat targetWeight = [targetWeightStr floatValue];
    CGFloat targetTime = [targetTimeStr floatValue];
    CGFloat nowWeight = [nowWeightStr floatValue];
    if (targetWeightStr == nil || targetTimeStr == nil) {
        self.targetView.reduceWeightLabel.text = @"每周减重 -- kg";
    } else {
    self.targetView.reduceWeightLabel.text = [NSString stringWithFormat:@"每周减重 %.1f kg", targetTime/(nowWeight - targetWeight)];
    }
    [self.view addSubview:self.targetView];
    [self.targetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakView.mas_centerX);
        make.centerY.mas_equalTo(weakView.mas_centerY).with.offset(-50);
        make.size.mas_equalTo(CGSizeMake(250, 250));
    }];
    
}

# pragma mark 设置折线图cell的保存点击方法
- (void)saveLineAction:(UIButton *)button{
    
    UITextField *textField = (UITextField *)[self.view viewWithTag:8000];
    NSString*documentsPathWeight = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPathWeight = [documentsPathWeight stringByAppendingPathComponent:@"targetWeight.txt"];
    [textField.text writeToFile:newFielPathWeight atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    UITextField *textFieldTime = (UITextField *)[self.view viewWithTag:8001];
    NSString*documentsPathTime = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPathTime = [documentsPathTime stringByAppendingPathComponent:@"targetTime.txt"];
    [textFieldTime.text writeToFile:newFielPathTime atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    NSString *targetWeightStr = [self sandBoxTargetWeight];
    NSString *targetTimeStr = [self sandBoxTargetTime];
    if ([targetWeightStr isEqualToString:@""] || [targetTimeStr isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"目标体重或者计划时长不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    UIVisualEffectView *effect = (UIVisualEffectView *)[self.view viewWithTag:1001];
    [effect removeFromSuperview];
    [self.targetView removeFromSuperview];
    [self.tableViewMine reloadData];
    
//    NSInteger timeInt = [targetTimeStr integerValue];
//    NSTimeInterval  interval = timeInt * 7 * 24 * 60 * 60 * 1;
//    NSTimer *myTimer = [NSTimer timerWithTimeInterval:interval target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
//    [[NSRunLoop  currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
    
}

//- (void)timerAction:(NSTimer *)timer{
//    //暂停
//    timer.fireDate = [NSDate distantFuture];
//}

#pragma mark 绿色button点击方法
- (void)clickLookChange:(UIButton *)button{
    [UIView animateWithDuration:2 animations:^{
        UILabel *label = (UILabel *)[self.view viewWithTag:2000];
        label.frame = CGRectMake(WIDTH_SCREEN-130, 80, 80, 20);
        label.text = @"相信自己!";
    } completion:^(BOOL finished) {
        // 想让这个UIView再回去在这里在写一个UIView的动画
        [UIView animateWithDuration:2 animations:^{
            UILabel *label = (UILabel *)[self.view viewWithTag:2000];
            label.frame = CGRectMake(WIDTH_SCREEN-130, 80, 80, 0);
        }];
    }];
}

#pragma mark 没设置体重时的button点击方法
- (void)click:(UIButton *)button{
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = [UIScreen mainScreen].bounds;
    effectView.tag = 1001;
    effectView.alpha = 0.8;
    [self.view addSubview:effectView];
    __weak typeof (self.view)weakView = self.view;
    self.viewSet = [[MineWeightDatePickerView alloc] init];
    self.viewSet.layer.cornerRadius = 10;
    self.viewSet.layer.borderWidth = 2;
    self.viewSet.layer.masksToBounds = YES;
    self.viewSet.textFieldNumber.delegate = self;
    self.viewSet.textFieldNumber.keyboardType = UIKeyboardTypeNumberPad;
    self.viewSet.textFieldNumber.tag = 3001;
    [self.viewSet.xButton addTarget:self action:@selector(xAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewSet.saveButton addTarget:self action:@selector(saveActionNoSet:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.viewSet];
    [self.viewSet mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakView.mas_centerX);
        make.centerY.mas_equalTo(weakView.mas_centerY).with.offset(-50);
        make.size.mas_equalTo(CGSizeMake(250, 200));
    }];
    // 给dateClickLabel添加个手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.viewSet.dateClickLabel addGestureRecognizer:tap];
    self.viewSet.dateClickLabel.userInteractionEnabled = YES;
    
}

#pragma mark 设置完体重的button点击方法
- (void)clickAction:(UIButton *)button{
    // *给视图添加一个毛玻璃效果
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = [UIScreen mainScreen].bounds;
    effectView.tag = 1001;
    effectView.alpha = 0.8;
    [self.view addSubview:effectView];
    __weak typeof (self.view)weakView = self.view;
    self.viewSet = [[MineWeightDatePickerView alloc] init];
    self.viewSet.layer.cornerRadius = 10;
    self.viewSet.layer.borderWidth = 2;
    self.viewSet.layer.masksToBounds = YES;
    self.viewSet.textFieldNumber.delegate = self;
    self.viewSet.textFieldNumber.keyboardType = UIKeyboardTypeNumberPad;
    self.viewSet.textFieldNumber.tag = 3001;
    [self.viewSet.xButton addTarget:self action:@selector(xAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewSet.saveButton addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.viewSet];
    
    [self.viewSet mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakView.mas_centerX);
        make.centerY.mas_equalTo(weakView.mas_centerY).with.offset(-50);
        make.size.mas_equalTo(CGSizeMake(250, 200));
    }];
    
    // 给dateClickLabel添加个手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.viewSet.dateClickLabel addGestureRecognizer:tap];
    self.viewSet.dateClickLabel.userInteractionEnabled = YES;
    
}

# pragma mark 让UITextField只允许输入数字
- (BOOL)validateNumberByRegExp:(NSString *)string {
     BOOL isValid = YES;
     NSUInteger len = string.length;
     if (len > 0) {
        NSString *numberRegex = @"^[0-9]*$";
         NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
         isValid = [numberPredicate evaluateWithObject:string];
    }
    return isValid;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return [self validateNumberByRegExp:string];
}

#pragma mark button保存方法
- (void)saveAction:(UIButton *)button{
    UITextField *textField = (UITextField *)[self.view viewWithTag:3001];
    if ([textField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请设置您的体重" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else {
    // 把体重存入本地
    NSString*documentsPathWeight = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPathWeight = [documentsPathWeight stringByAppendingPathComponent:@"weight.txt"];
    [textField.text writeToFile:newFielPathWeight atomically:YES encoding:NSUTF8StringEncoding error:nil];
    // 把身高存入本地
    NSString*documentsPathHeight = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPathHeight = [documentsPathHeight stringByAppendingPathComponent:@"height.txt"];
    [self.BMIView.heightNumberTextField.text writeToFile:newFielPathHeight atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    MineNoSetTVCell *cell = (MineNoSetTVCell *)[self.view viewWithTag:1000];
    [cell removeFromSuperview];
    [self.tableViewMine reloadData];
    [self.targetView removeFromSuperview];
    [self.viewSet removeFromSuperview];
    UIVisualEffectView *effect = (UIVisualEffectView *)[self.view viewWithTag:1001];
    [effect removeFromSuperview];
    [self.BMIView removeFromSuperview];
    }
}

# pragma mark 没设置之前的cell的button保存方法
- (void)saveActionNoSet:(UIButton *)button{
    // 把体重存入本地
    UITextField *textField = (UITextField *)[self.view viewWithTag:3001];
    NSString*documentsPathWeight = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPathWeight = [documentsPathWeight stringByAppendingPathComponent:@"weight.txt"];
    [textField.text writeToFile:newFielPathWeight atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if ([textField.text isEqualToString:@""]) {
        _isSelected = NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请设置您的体重" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        _isSelected = YES;
    }
    // 把身高存入本地
    NSString*documentsPathHeight = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPathHeight = [documentsPathHeight stringByAppendingPathComponent:@"height.txt"];
    [self.BMIView.heightNumberTextField.text writeToFile:newFielPathHeight atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    MineNoSetTVCell *cell = (MineNoSetTVCell *)[self.view viewWithTag:1000];
    [cell removeFromSuperview];
    [self.tableViewMine reloadData];
    [self.targetView removeFromSuperview];
    [self.viewSet removeFromSuperview];
    UIVisualEffectView *effect = (UIVisualEffectView *)[self.view viewWithTag:1001];
    [effect removeFromSuperview];
    [self.BMIView removeFromSuperview];
}

#pragma mark button x方法
- (void)xAction:(UIButton *)button{
    [self.viewSet removeFromSuperview];
    UIVisualEffectView *effect = (UIVisualEffectView *)[self.view viewWithTag:1001];
    [effect removeFromSuperview];
    [self.targetView removeFromSuperview];
    [self.BMIView removeFromSuperview];
}

#pragma mark 日期label的点击手势
- (void)tapAction:(UITapGestureRecognizer *)tap{
    self.datePickerView = [[YJW_DatePickerView alloc] init];
    [self.datePickerView.buttonSure addTarget:self action:@selector(sureAction:) forControlEvents:1<<6];
    [self.datePickerView.buttonCancel addTarget:self action:@selector(cancelAction:) forControlEvents:1<<6];
    self.datePickerView.tag = 7000;
    [self.view addSubview:self.datePickerView];
    [self.datePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(WIDTH_SCREEN, HEIGHT_SCREEN));
    }];
}

- (void)cancelAction:(UIButton *)button{
    [self.datePickerView removeFromSuperview];
}

- (void)sureAction:(UIButton *)button{
    NSString *timestamp = [self.datePickerView.dateForMatter stringFromDate:self.datePickerView.datePicker.date];
    self.viewSet.dateClickLabel.text = timestamp;
    [self.datePickerView removeFromSuperview];
}

#pragma mark viewWillAppear
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self createStepNumber];
    [self.tableViewMine reloadData];
}

# pragma mark 在沙盒里取值(在cell里调用并赋值)体重
- (NSString *)sandBoxWeight{
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPath = [documentsPath stringByAppendingPathComponent:@"weight.txt"];
    NSString *weightStr = [NSString stringWithContentsOfFile:newFielPath encoding:NSUTF8StringEncoding error:nil];
    return weightStr;
}

#pragma mark 在沙盒里取值(在cell里调用并赋值)身高
- (NSString *)sandBoxHeight{
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPath = [documentsPath stringByAppendingPathComponent:@"height.txt"];
    NSString *heightStr = [NSString stringWithContentsOfFile:newFielPath encoding:NSUTF8StringEncoding error:nil];
    return heightStr;
}

# pragma mark 在沙盒里取值(去给每周减重赋值) 目标体重
- (NSString *)sandBoxTargetWeight{
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPath = [documentsPath stringByAppendingPathComponent:@"targetWeight.txt"];
    NSString *targetWeightStr = [NSString stringWithContentsOfFile:newFielPath encoding:NSUTF8StringEncoding error:nil];
    return targetWeightStr;
}

# pragma mark 在沙盒里取值(去给每周减重赋值) 目标时长
- (NSString *)sandBoxTargetTime{
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString*newFielPath = [documentsPath stringByAppendingPathComponent:@"targetTime.txt"];
    NSString *targetTime = [NSString stringWithContentsOfFile:newFielPath encoding:NSUTF8StringEncoding error:nil];
    return targetTime;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
